package com.ruoyi.books.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.system.service.impl.SysUserServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.books.mapper.BookMapper;
import com.ruoyi.books.domain.Book;
import com.ruoyi.books.service.IBookService;

import javax.validation.Validator;

/**
 * 图书信息Service业务层处理
 *
 * @author lmh
 * @date 2024-09-28
 */
@Service
public class BookServiceImpl implements IBookService {
    private static final Logger log = LoggerFactory.getLogger(BookServiceImpl.class);
    @Autowired
    private BookMapper bookMapper;


    @Autowired
    protected Validator validator;

    /**
     * 查询图书信息
     *
     * @param id 图书信息主键
     * @return 图书信息
     */
    @Override
    public Book selectBookById(Long id) {
        return bookMapper.selectBookById(id);
    }

    /**
     * 查询图书信息列表
     *
     * @param book 图书信息
     * @return 图书信息
     */
    @Override
    public List<Book> selectBookList(Book book) {
        return bookMapper.selectBookList(book);
    }

    /**
     * 新增图书信息
     *
     * @param book 图书信息
     * @return 结果
     */
    @Override
    public int insertBook(Book book) {
        book.setCreateTime(DateUtils.getNowDate());
        book.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
        return bookMapper.insertBook(book);
    }

    /**
     * 修改图书信息
     *
     * @param book 图书信息
     * @return 结果
     */
    @Override
    public int updateBook(Book book) {
        book.setUpdateTime(DateUtils.getNowDate());
        book.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        return bookMapper.updateBook(book);
    }

    /**
     * 批量删除图书信息
     *
     * @param ids 需要删除的图书信息主键
     * @return 结果
     */
    @Override
    public int deleteBookByIds(Long[] ids) {
        return bookMapper.deleteBookByIds(ids);
    }

    /**
     * 删除图书信息信息
     *
     * @param id 图书信息主键
     * @return 结果
     */
    @Override
    public int deleteBookById(Long id) {
        return bookMapper.deleteBookById(id);
    }

    @Override
    public String importBook(List<Book> bookList, boolean updateSupport) {
        if (StringUtils.isNull(bookList) || bookList.size() == 0) {
            throw new ServiceException("导入图书数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (Book book : bookList) {
            try {
                // 验证是否存在这个用户
                Book existBook = bookMapper.selectBookByBookName(book);
                if (existBook == null)// 不存在
                {
                    BeanValidators.validateWithException(validator, book);
                    book.setCreateTime(DateUtils.getNowDate());
                    book.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
                    bookMapper.insertBook(book);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、图书名称 " + book.getName() + " 导入成功");
                } else if (updateSupport) {
                    BeanValidators.validateWithException(validator, book);
                    book.setUpdateTime(DateUtils.getNowDate());
                    book.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
                    bookMapper.updateBook(book);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、图书名称 " + book.getName() + " 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、图书名称 " + book.getName() + " 已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、图书名称 " + book.getName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
